//
//  CLComposeToolBar.swift
//  CLWeibo
//
//  Created by 陈亮 on 16/7/18.
//  Copyright © 2016年 陈亮. All rights reserved.
//

import UIKit

//发微博界面  toolbar的五个按钮的枚举值
enum CLComeposeToolBarButtonType:Int {
    case Picture = 0
    case Mention = 1
    case Trend = 2
    case Emoticon = 3
    case Add = 4
}
//UIStackView 自身不具备渲染效果 依赖子控件的显示
class CLComposeToolBar: UIStackView {

    
    //定义表情键盘按钮
    var emoiconButton:UIButton?
    //定义点击toolbar按钮的闭包
    var selectedToolBarButtonClosure:((type:CLComeposeToolBarButtonType)->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpUI(){
        
        //设置布局方式
        axis = .Horizontal
        //设置子控件填充方式
        distribution = .FillEqually
        //添加子控件
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emoiconButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
    }

    //添加子控件方法
    private func addChildButton(imageName:String,type :CLComeposeToolBarButtonType) -> UIButton{
        
        let button = UIButton()
        //获取枚举值的原始值
        button.tag = type.rawValue
        //添加点击事件
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        //设置图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        button.adjustsImageWhenHighlighted = false
        //添加控件
        addArrangedSubview(button)
       
        
    
        return button
        
    }
    //toolbar点击事件
    @objc private func buttonAction(button:UIButton){
        
        //通过枚举原始值获取枚举成员
        let type = CLComeposeToolBarButtonType(rawValue: button.tag)!
        
        //执行闭包回调
        selectedToolBarButtonClosure?(type: type)
        
    }
    
    func showEmoiconButtonIcon (isEmoicon :Bool){
        
        if isEmoicon {
            //系统键盘
            emoiconButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emoiconButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
            
        }else {
            //显示的是emoji键盘
            emoiconButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emoiconButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
        
        
        
    }
    
    
}
